<?php

namespace App\Http\Controllers\Home;

use App\Helpers\Helpers;
use App\Model\UserPoint;
use App\Model\UserWithdraw;
use App\Model\WeChatFromId;
use Illuminate\Http\Request;

class WithdrawController extends BaseController
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index()
    {
        //
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        if ($formid = request('formid')){
            WeChatFromId::createFromForm($formid,$this->loginUser()->weixinId,1);
        }

        $user = $this->loginUser();
        $input = $request->all();
        $bool = $this->requestValidate($request->all(),[
            'card_username' => 'required|min:2|max:15',
            'card_bank' => 'required|max:50',
            'card_number' => 'required|max:50',
            'bank_subbranch_name' => 'required',
            'request_amount' => 'required|numeric|min:100',
        ],[
            'card_username.required' => '开户账户必填',
            'card_number.required' => '银行卡号码必填',
            'card_bank.required' => '银行必填',
            'bank_subbranch_name.required' => '银行卡具体开户支行必填',
            'request_amount.required' => '提现金额必填',
        ]);

        if (!$bool) return $this->failed($this->error);

        $request_amount  = $input['request_amount'];

        $fee = $request_amount*0.25;


        if ($request_amount+$fee>$user->point){
            return $this->failed('超出可提现金额');
        }

        $uw = new UserWithdraw();
        $uw->user_id = $user->id;
        $uw->state = 0;
        $uw->transaction_no = Helpers::createSerial('user_withdraws','transaction_no',date('Ymd'));
        $uw->username = $user->name?$user->name:($user->mobile?$user->mobile:'');
        $uw->mobile = $user->mobile?$user->mobile:'';
        $uw->card_username = $input['card_username'];
        $uw->card_bank = $input['card_bank'];
        $uw->card_number = $input['card_number'];
        $uw->bank_subbranch_name = $input['bank_subbranch_name'];
        $uw->request_amount = $input['request_amount'];
        $uw->request_time = date('Y-m-d H:i:s');
        $uw->fee = $fee;
        $uw->amount_fee = $uw->request_amount+$uw->fee;
        $uw->current_point = $user->point;
        $uw->save();

        UserPoint::createOneRecord($uw->user_id, -1*$uw->amount_fee,'提现至银行卡(含手续费'. $uw->fee.')',
            'withdraw',$uw->id,['state'=>'withdraw-pending','stateName'=>"待审核",'ok'=>1]);

        return $this->success(['transaction_no'=>$uw->transaction_no,'msg'=>'已提交申请']);
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        $item = UserWithdraw::find($id);
        return $this->success($item);
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }
}
